package com.android.dumiaogou.fragment.member;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.dumiaogou.R;
import com.android.dumiaogou.fragment.BaseLazyFragment;
import com.android.dumiaogou.model.member.AboutUs;
import com.android.dumiaogou.model.member.AboutUsCallback;
import com.android.dumiaogou.utils.ConstantsUrl;
import com.android.dumiaogou.utils.HttpUtils;
import com.android.dumiaogou.utils.ObjectUtils;
import com.android.dumiaogou.utils.update.UpVersionDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 评价
 */
public class Fragment_Mine_AboutUs extends BaseLazyFragment {


    @BindView(R.id.aboutus_content)
    TextView aboutusContent;
    @BindView(R.id.aboutus_phone)
    TextView aboutusPhone;
    @BindView(R.id.aboutus_version)
    TextView aboutusVersion;
    @BindView(R.id.aboutus_iv)
    ImageView aboutus_iv;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;


    private boolean networkConnected;
    private String store_mobile;


    public Fragment_Mine_AboutUs() {


    }


    public static Fragment_Mine_AboutUs newInstance() {
        Fragment_Mine_AboutUs fragment = new Fragment_Mine_AboutUs();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        GetData();

    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_aboutus, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        String versionCode = ObjectUtils.getVersionName(getActivity());
        aboutusVersion.setText("当前版本" + versionCode);
        return view;
    }


    private void initUI() {
        String versionCode = ObjectUtils.getVersionName(mcontext);


    }


    @OnClick({R.id.aboutus_update, R.id.click_resetnetwork_refresh,R.id.aboutus_phone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aboutus_update:
                UpVersionDialog upVersionDialog = new UpVersionDialog();
                upVersionDialog.show(mcontext.getFragmentManager(), "upVersionDialog");
                break;
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                GetData();
                break;
            case  R.id.aboutus_phone:
                store_mobile = aboutusPhone.getText().toString().trim();
                Intent intent1 = new Intent(Intent.ACTION_DIAL);
                intent1.setData(Uri.parse("tel:"+store_mobile));
                startActivity(intent1);
                break;
        }
    }


    //获取列表数据
    private void GetData() {

        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_ABOUTUS;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HttpUtils.get(address, null, new AboutUsCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
               public void onResponse(AboutUs response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        AboutUs.DataBean data = response.getData();
                        String  aboutus = data.getContent();
                        aboutusContent.setText(aboutus);
                        aboutusPhone.setText(data.getTel());
//                        aboutusVersion.setText("当前版本" + data.getVersion());
                        ObjectUtils.photo2(mcontext, data.getLogo(),aboutus_iv);
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());

                    }
                }

//
//                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
//                    if (response.getCode() == 0) {
//                        aboutusContent.setText(response.getData().getContent());
//                        aboutusPhone.setText(response.getData().getTel());
//                        ReleaseManager.printLog("URL",""+response.getData().getLogo());
//                        aboutusVersion.setText("当前版本" + response.getData().getVersion());
//                        ObjectUtils.photo2(mcontext,response.getData().getLogo(),aboutus_iv);
//                    } else {
//                        noData.setVisibility(View.VISIBLE);
//                        noDataTv.setVisibility(View.VISIBLE);
//                        noDataTv.setText(response.getMsg());
//
//                    }
//                }

            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }
}
